package jh.com.jhappbf.assay;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.ab.activity.AbActivity;
import com.ab.view.titlebar.AbTitleBar;
import com.android.volley.Request.Priority;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;

import org.json.JSONObject;

import java.util.ArrayList;

import androidx.core.content.ContextCompat;
import jh.com.jhappbf.R;
import jh.com.jhappbf.httputils.VolleyCallWebService;
import jh.com.jhappbf.httputils.VolleySingleton;
import jh.com.jhappbf.utils.JsonHelper;

/**
 * @author Administrator
 */
public class ShowAssayImgActivity extends AbActivity implements OnClickListener {

    private ImageView assayImg;
    private String id = "";
    private ProgressBar mProgressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View mContentView = getLayoutInflater().inflate(R.layout.activity_show_assay_img, null);
//        mErrorView = getLayoutInflater().inflate(R.layout.net_error_activity, null);
//		mAppcontext  = getApplicationContext();
        setAbContentView(mContentView);
        Intent i = getIntent();
        id = i.getStringExtra("id");
//		mSettingModel = i.getParcelableExtra("setting");
        AbTitleBar mTitleBar = getTitleBar();
        mTitleBar.setTitleText("化验图片查看");
        mTitleBar.setLogo(R.drawable.button_selector_back);
//        mTitleBar.setTitleBarBackground(R.drawable.top_bg);
        mTitleBar.setTitleBarBackgroundColor(ContextCompat.getColor(this, R.color.orange));
        mTitleBar.setTitleTextMargin(10, 0, 0, 0);
        mTitleBar.setLogoLine(R.mipmap.line);
        assayImg = findViewById(R.id.assay_img);
        assayImg.setOnClickListener(this);
        mProgressBar = findViewById(R.id.assay_img_progress);

        getImgFromNet();
//		init();
    }

    private void getImgFromNet() {
        String methodName = "getAssayImg";
        ArrayList<String> param = new ArrayList<>();

        param.add("id");
        ArrayList<String> value = new ArrayList<>();
        if (id.length() == 0) {
            return;
        }
        value.add(id);
//        Log.e("wwh", "id=---------" + id);
        Listener<String> listener = response -> {
            mProgressBar.setVisibility(View.GONE);
//                Log.e("wwh", response);
            JSONObject obj = JsonHelper.getJsonObject(response);
            if (obj != null) {
                String result = obj.optString("result");

                //	byte[] data = decoder.decodeBuffer(result);
                //	saveImage(field, id, data);
                if (result.length() > 0) {
                    byte[] data = Base64.decode(result, Base64.DEFAULT);
                    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                    assayImg.setImageBitmap(bitmap);
                } else {
                    assayImg.setImageResource(R.mipmap.load_error);
                }

            }
        };

        ErrorListener errorListener = error -> {
            mProgressBar.setVisibility(View.GONE);
            assayImg.setImageResource(R.mipmap.load_error);
        };

        VolleyCallWebService webService = new VolleyCallWebService(methodName,
                param, value, listener, errorListener, Priority.NORMAL);
        VolleySingleton.getVolleyInstance(this).addRequest(webService);
    }

    @Override
    public void onClick(View arg0) {

        mProgressBar.setVisibility(View.VISIBLE);
        getImgFromNet();
    }

}